﻿namespace CAION2.ViewModels
{
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Xml.Linq;

    using Classes;

    public class StigmaCardViewModel : ViewModelBase
    {
        #region Vars

        private byte placeIndex;

        #endregion


        #region Properties

        public Dictionary<string, DataRow> Stigmas { get; private set; }
        public string CurrentStigmaKey { get; set; }

        private byte CurrentCharacterLevel { get; set; }
        private bool CurrentCharacterRace { get; set; }

        public byte PlaceIndex
        {
            get
            {
                return placeIndex;
            }
            set
            {
                if (Equals(placeIndex, value)) return;
                placeIndex = value;
            }
        }

        #endregion

        public StigmaCardViewModel()
        {
            Stigmas = new Dictionary<string, DataRow>();
        }

        public StigmaCardViewModel(Dictionary<string, DataRow> stigmas, byte currentCharacterLevel, byte index, string currentStigmaKey)
        {
            Stigmas = stigmas;
            PlaceIndex = index;
            CurrentCharacterLevel = currentCharacterLevel;
            CurrentStigmaKey = currentStigmaKey;
        }

        public StigmaCardViewModel(DataRow currentStigma, byte currentCharacterLevel, byte index, bool currentCharacterRace)
        {
            if (currentStigma == null)
            {
                Stigmas = new Dictionary<string, DataRow>();
                return;
            }

            PlaceIndex = index;
            CurrentCharacterLevel = currentCharacterLevel;
            CurrentCharacterRace = currentCharacterRace;
            Stigmas = StigmaSet.CreateStigmaListForStigma(currentStigma["skill_group_name"].ToString(), CurrentCharacterRace);
            CurrentStigmaKey = currentStigma["name"].ToString();
        }

        public void AddStigma(DataRow row)
        {
            Stigmas.Add(row["name"].ToString(), row);
        }

        public void SetCurrentStigma(int index)
        {
            if (index >= Stigmas.Count) return;
            CurrentStigmaKey = Stigmas.Keys.ElementAt(index);
        }

        public DataRow GetCurrentStigma()
        {
            if (Stigmas == null || Stigmas.Count == 0 || string.IsNullOrEmpty(CurrentStigmaKey)) return null;
            return Stigmas.ContainsKey(CurrentStigmaKey) ? Stigmas[CurrentStigmaKey] : null;
        }

        public XElement GetCardElementForSaveToFile()
        {
            XElement xCard = new XElement("Stigma");

            xCard.Add(new XAttribute("Index", PlaceIndex));
            if (CurrentStigmaKey != null) xCard.Add(new XAttribute("CurrentKey", CurrentStigmaKey));
            
            return xCard;
        }
    }
}
